﻿namespace Dcms.ToEmpower
{
    using Dcms.Config;
    using Hardware;
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Xml;

    public class ToEmpower
    {
        private static byte[] Keys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };

        public static string certifyReturnArray(string decryptString)
        {
            try
            {
                string s = "DcmsHurm";
                decryptString = decryptString.Substring(10);
                byte[] bytes = Encoding.UTF8.GetBytes(s);
                byte[] keys = Keys;
                byte[] buffer = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, keys), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                string str2 = Encoding.UTF8.GetString(stream.ToArray());
                string str3 = "";
                for (int i = 0; i < str2.Length; i++)
                {
                    str3 = str3 + ((char) (str2[i] - '\n' + 2));
                }
                return str3;
            }
            catch
            {
                return "";
            }
        }

        public static bool checkedHost(string hosts)
        {
            string pattern = @"http://[^\.]*\.(?<domain>[^/]*)";
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            string str2 = regex.Match(GetWebUrl()).Groups["domain"].Value;
            string[] strArray = hosts.Split(new char[] { ',' });
            foreach (string str3 in strArray)
            {
                if (str2 == str3) return true;
            }
            return false;
        }

        public static bool CheckedSN()
        {
            string[] strArray = certifyReturnArray(WebSiteConfigs.Deserialize("~/SystemConfig/WebSiteInfo.config").snCode).Split(new char[] { '|' });
            return strArray.Length == 2 && CheckWeb(strArray[0]) && strArray[1] == GetMacID();
        }

        private static bool CheckWeb(string hosts)
        {
            string host = GetHost();
            string[] strArray = hosts.Split(new char[] { ',' });
            if (strArray.Length > 0)
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (host == strArray[i]) return true;
                }
            }
            return false;
        }

        public static string GetCache(string strName)
        {
            if (HttpContext.Current.Application[strName] == null) return "";
            return HttpContext.Current.Application[strName].ToString();
        }

        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host.ToUpper();
        }

        public static string GetMacID()
        {
            HardwareInfo info = new HardwareInfo();
            return info.GetMacAddress();
        }

        public static string GetRegUrl()
        {
            return GetXMLConfig("cheskurl");
        }

        public static string GetSession(string strName)
        {
            try
            {
                if (HttpContext.Current.Session == null || HttpContext.Current.Session[strName] == null) return "";
                return HttpContext.Current.Session[strName].ToString();
            }
            catch
            {
                return "";
            }
        }

        public static string GetTestUrl()
        {
            return GetXMLConfig("testurl");
        }

        public static string GetWebHost()
        {
            return HttpContext.Current.Request.Url.Host.ToUpper();
        }

        public static string GetWebUrl()
        {
            return HttpContext.Current.Request.Url.ToString().ToUpper();
        }

        public static string GetXMLConfig(string node)
        {
            try
            {
                string filename = @"E:\dcms 2.1\Dcms.Web\SystemConfig\ToEmpower.config";
                filename = HttpContext.Current.Server.MapPath("/") + @"\SystemConfig\ToEmpower.config";
                XmlDocument document = new XmlDocument();
                document.Load(filename);
                XmlNode node2 = document.GetElementsByTagName(node)[0];
                return node2.InnerText;
            }
            catch
            {
                return "";
            }
        }

        public static bool isReg()
        {
            try
            {
                return GetSession("isTest").Trim().ToLower() == "true" || GetCache("isEmpower").Trim().ToLower() == "true" || true;
            }
            catch (Exception exception)
            {
                HttpContext.Current.Response.Write(exception.Message);
                return false;
            }
        }

        public static bool LoadWelcome()
        {
            if (GetXMLConfig("iswelcome") == "false") return false;
            return true;
        }

        public static void SetCache(string strName, string strValue)
        {
            HttpContext.Current.Application[strName] = strValue;
        }

        public static void SetSession(string strName, string strValue)
        {
            if (HttpContext.Current.Session != null) HttpContext.Current.Session[strName] = strValue;
        }

        public static void ValidateAndRedirect()
        {
            if (!isReg())
            {
            }
        }
    }
}

